1. /* slfdivnw.cpp by K.Tsuru */
  2. // function ID = 214 DRADIX
  3. /*************************************************
  4. SLong class only
  5. This is a protected member function which user cannot access.
  6. m/n by Newton method m and n both positive.
  7. q = m/n, r = m - q*n;
  8. m > n > 0, n > 1 etc. has been checked in LLDiv().
  9. *************************************************/
  10. #ifndef SN_H
  11. #include "sn.h"
  12. #endif
  13. static const char* const func = "NewtonLLDiv";
  14. Ldiv_t SLong::NewtonLLDiv(const SLong& m, const SLong& n, bool needRem){
  15. if( (m.Head() < n.Head()) || (m.Sign() <= 0) || (n.Sign() <= 0) ){
  16. m.SetError(m.SYNTAX_ERR, func, 214);
  17. }
  18. //check the radix
  19. if( (m.Radix() != DRADIX) || (n.Radix() != DRADIX) ){
  20. m.SetError(m.RADIX_ERR, func, 214);
  21. }
  22. uint inv_fig = m.Head()-n.Head()+2u; //necessary figures of 1/n
  23. RealSize C;
  24. C.SetEffFig(inv_fig);
  25. // SLong ---> SDouble conversion
  26. SDouble M(m), N(n);
  27. SLong q, r, one(1.0);
  28. M = M/N;
  29. q = M; // SDouble --> SLong conversion
  30. C.SetEffFig(0);
  31. //It corrects the quotient and remainder.
  32. //Almost an exact result is obtained.
  33. r = m - n*q;
  34. if(r.Sign(214) < 0){
  35. while(r.Sign(214) < 0){ // q is too large.
  36. q -= one; r += n;
  37. }
  38. } else {
  39. while(r >= n){ // q is too small.
  40. q += one; r -= n;
  41. }
  42. }
  43. if(!needRem) return q;
  44. return Ldiv_t(q, r); //The verify is done in LLDiv.
  45. }

slmdivnw.cpp : last modifiled at 2016/07/28 16:22:23(1,389 bytes)
created at 2017/10/07 10:26:50
The creation time of this html file is 2017/11/09 14:52:03 (Thu Nov 09 14:52:03 2017).